package iSOCK::Controller::Test;

use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller' }

=head1 NAME

iSOCK::Controller::Test - Test Controller for iSOCK

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

sub _bounce_to : Private {
  my ( $self, $c, $to ) = @_;
  
  $c->response->redirect ( $c->uri_for ( $to ) );
  $c->detach;
}

sub _bounce_to_login : Private {
  my ( $self, $c ) = @_;
  
  $self->_bounce_to ( $c, '/login' );
}

sub _bounce_to_logout : Private {
  my ( $self, $c ) = @_;
  
  $self->_bounce_to ( $c, '/logout' );
}

sub _check_user : Private {
  my ( $self, $c ) = @_;
  
  if ( my $user_r = $c->model ( 'App' )->user_r ( $c ) ) {
    my $role = {
      can_test_js => $user_r->assert_user_role ( 'can_test_js' ),
    };

    unless ( $role->{can_test_js} ) {
      $self->_bounce_to_logout ( $c );
    }

    $c->stash ( role => $role );
  }
  else {
    $self->_bounce_to_login ( $c );
  }
}

sub index : Path {
  my ( $self, $c ) = @_;
  
  $self->_check_user ( $c );
  
  $c->stash ( test => 'harness', title => 'Test' );
}

sub case : LocalRegex('^(bot)$') {
  my ( $self, $c ) = @_;
  
  $self->_check_user ( $c );
  
  my ( $case ) = ( split '/', $c->request->path )[-1];
  
  $c->stash ( test => 'case', title => "Test ($case)", case => $case, template => $c->request->path );
}

sub end : ActionClass('RenderView') {}

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
